﻿using System;
using System.Collections.Generic;
using System.Text;

namespace Weazel.Willows.Model
{
	public partial class QueueReaderInterface 
		:  ChannelReaderInterface, IQueueReaderInterface
	{
		protected IQueueConnectorReader attachedQueue;
		public IQueueConnectorReader AttachedQueue
		{
			get
			{
				if (attachedQueue == null)
				{
					string message =
						string.Format("No Queue attached to Queue connector reader interface '{0}'.", Name);

					throw new NullReferenceException(message);
				}

				return attachedQueue;
			}
			set
			{
			//	if (attachedQueue != null)
			//		throw new Exception("Queue already bound");

				attachedQueue = value;
			}
		}

		public virtual void WriteChannelHierarchy(System.CodeDom.Compiler.IndentedTextWriter writer)
		{
			writer.WriteLine("Queue reader interface '{0}'", LocalName);			
		}

		private Module reader;

		public virtual List<Module> GetReadingDatapaths()
		{
			if (reader == null)
			{
				string message =
					string.Format("GetReadingDatapaths(): reader not set ({0})", Name);

				throw new NullReferenceException(message);
			}

			List<Module> result = new List<Module>();
			result.Add(reader);
			return result;
		}

		public override Module GetWritingDatapath()
		{
			return attachedQueue.GetWritingDatapath();
		}

		public void SetReadingDatapath(Module datapath)
		{
			this.reader = datapath;
		}

		public bool HasAttachedChannel
		{
			get { return attachedQueue != null; }
		}

		public bool HasAttachedReader
		{
			get { return false; }
		}

		#region Constructors
		public QueueReaderInterface(Module parent, QueueConnectorReaderInterface readerInterface, string uniqueName)
			: base(parent, readerInterface.TypeRegistry, readerInterface.Type, readerInterface.LocalName, uniqueName)
		{
			this.attachedQueue = readerInterface;
			this.attachedQueue.BindReader(this);
		}

		public QueueReaderInterface(Module parent, Queue queue, string uniqueName)
			: base(parent, queue.TypeRegistry, queue.Type, queue.LocalName, uniqueName)
		{
			this.attachedQueue = queue;
			this.attachedQueue.BindReader(this);
		}

		protected QueueReaderInterface(Module parent, TypeRegistry typeRegistry, Type type, string name, string uniqueName)
			: base(parent, typeRegistry, type, name, uniqueName)
		{			
		}
		#endregion

		
	}
}
